package com.example.eshop_ssm.service.impl;

import com.example.eshop_ssm.entity.TbBookInfo;
import com.example.eshop_ssm.mapper.BookTrashMapper;
import com.example.eshop_ssm.service.BookTrashService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BookTrashServiceImpl implements BookTrashService {
    @Autowired
    private BookTrashMapper bookTrashMapper ;
    @Override
    public Map<String, Object> selectBookInfo(String page) {

        List<TbBookInfo> tbBookList= new ArrayList<>();
        //分页---页数
        if ((page!=null && page!="")){
            if (Integer.parseInt(page)>1){
              tbBookList= bookTrashMapper.selectBookInfo((Integer.parseInt(page)-1)*10);
            }else {
                tbBookList=bookTrashMapper.selectBookInfo(0);
            }
        }else {
            tbBookList= bookTrashMapper.selectBookInfo(0);
        }

        //查询总条数
        Integer integer = bookTrashMapper.selectCount();
        //总页数
        Integer pageCount = integer%10==0? integer/10 : integer/10+1;


        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("tbBookList",tbBookList); //书本信息
        objectMap.put("pageCount",pageCount); //记录总页数
        return  objectMap;
    }

    @Override
    public Integer deleteBook(Integer book_id) {
        if ((book_id!=null)){
           return bookTrashMapper.deleteBook(book_id);
        }
        return -1;
    }

    @Override
    public Integer updateBook(String book_id) {
        if ((book_id!=null && book_id!="")){
            return bookTrashMapper.updateBook(Integer.parseInt(book_id));
        }
        return -1;
    }

    @Override
    public Integer restoreOrDelete(String is_garbageOrDelete, String bookIds) {

        if("delete".equals(is_garbageOrDelete)){
            //执行删除
            return bookTrashMapper.deleteBooks(bookIds.split(","));
        }else if("is_garbage".equals(is_garbageOrDelete)){
            //执行复原
            return bookTrashMapper.updateBooks(bookIds.split(","));
        }

        return null;
    }

}
